package com.example.mapp;

import com.example.entity.SysVersion;
import com.example.entity.authority.SystemUser;
import com.example.ext.entity.ExtSysUser;
import com.example.mapp.buildSql.SysUserMappSql;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/8/13.
 */
@Mapper
public interface SysUserMapp {

    /**
     * 根据用户id查询用户信息
     * @param userId
     * @return
     * @throws Exception
     */
    @SelectProvider(
            type = SysUserMappSql.class,
            method = "getSysUser")
    List<ExtSysUser> findUserList(Long userId) throws Exception;

    /**
     * 查询用户的功能
     * @param userId
     * @return
     * @throws Exception
     */
    @SelectProvider(
            type = SysUserMappSql.class,
            method = "findUserFunction")
    List<ExtSysUser> findUserFunction(String userId) throws Exception;


    /**
     * 查询角色拥有的功能
     * @param roleId
     * @return
     * @throws Exception
     */
    @SelectProvider(
            type = SysUserMappSql.class,
            method = "findUserFunctionByRoleId")
    List<ExtSysUser> findUserFunctionByRoleId(String roleId) throws Exception;


    @Select(" SELECT  DISTINCT e.author_id as authorId from sys_user as a  " +
            " left join sys_user_role as b on a.id = b.user_id   " +
            " left join system_role as c on b.role_id = c.id   " +
            " left join sys_role_author as d on d.role_id = c.id "  +
            " left join system_author as e on d.author_id = e.id  " +
            " where e.sys_type='pc' and e.author_type='func' and a.user_code=#{userName}")
    List<String>  findFuncSID(String userName) throws  Exception;

    /**
     * 根据用户登录名查询用户信息
     * @param userName
     * @return
     * @throws Exception
     */
    @Select("select s.user_code as userCode,s.type, s.user_name as userName, s.user_state userState, s.card_type cardType, s.mark," +
            "s.phone, s.mobile, s.card_number cardNumber, s.user_state as userState," +
            "s.version, s.work_number workNumber,s.email,s.password, s.id,p.id as partyId,p.party_name,p.party_addr," +
            "p.party_type partyType, p.parent_id as partyParentId,p.party_level as partyLevel," +
            "b.role_name as roleName,b.id as roleId" +
            " from sys_user as s " +
            " left join party_user pu on pu.user_id=s.id " +
            " left join party as p on p.id = pu.party_id  " +
            "LEFT join sys_user_role as a on a.user_id = s.id " +
            "LEFT JOIN system_role as b on a.role_id = b.id where s.user_code = #{userName}")
    ExtSysUser findUserByUserName(String userName) throws Exception;


    @Select(" select a.* from sys_user as a left join party_user as b on a.id = b.user_id " +
            "where a.user_name=#{userName} and b.party_id = #{partyId}")
    List<ExtSysUser> findUserByPartyIdAndUserName(ExtSysUser extSysUser)throws Exception;

    /**
     * 获取用户列表信息
     * @return
     * @throws Exception
     */
    @SelectProvider(
            type = SysUserMappSql.class,
            method = "findSysUserList"
    )
    List<ExtSysUser> findSysUserList(ExtSysUser systemUser) throws Exception;

    @Insert(" insert into sys_user " +
            "(user_code,password,type,user_name,role_type_id,card_type,mobile,email,card_Number,card_Type_Number,file_List,face_url,role_id, user_state, firstgt, phone, work_number) values " +
            "(#{userCode},#{password},#{type},#{userName},#{roleTypeId},#{cardType},#{mobile},#{email},#{cardNumber},#{cardTypeNumber},#{fileList},#{faceUrl},#{roleId},#{userState},#{firstgt},#{phone},#{workNumber})")
    @SelectKey(statement="select last_insert_id()",keyProperty = "id",keyColumn = "id",before = false,resultType = Long.class)
    public int add(ExtSysUser extSysUser) throws Exception;

    @Select("select s.*,c.id as partyId ,c.party_name,c.party_addr from " +
            " sys_user as s  left join party_user as b on b.user_id = s.id " +
            " LEFT JOIN party as c on b.party_id = c.id  where s.id = #{id} ")
    ExtSysUser findById(String id) throws Exception;

    @Select("select * from sys_user where user_name = #{userName} ")
    ExtSysUser findByUserName(String userName) throws Exception;

    @Update(" update sys_user set user_code=#{userCode},password=#{password},type=#{type},user_name=#{userName},role_type_id=#{roleTypeId}" +
            ",card_type=#{cardType},mobile=#{mobile},email=#{email},role_id=#{roleId},card_Number=#{cardNumber}," +
            "card_Type_Number=#{cardTypeNumber},file_List=#{fileList},face_url=#{faceUrl},firstgt=#{firstgt},phone=#{phone},work_number=#{workNumber} where id=#{id} ")
    public int update(SystemUser systemUser) throws Exception;

    @Update("update sys_user set face_url=#{faceUrl} where id=#{id}")
    public int updateFaceUrl(SystemUser systemUser) throws Exception;

    @Delete(" delete from sys_user where id=#{id} ")
    public int delete(String id) throws Exception;

    @Select(" SELECT s.* from sys_user as s inner join party_user as p on s.id = p.user_id where p.party_id = #{partyId}")
    List<SystemUser> queryByPartyId(String partyId) throws Exception;

    @Select(" select * from ( select user_code from sys_user union " +
            "select user_code from user_check where status!='3' union " +
            "select user_code from party_check where status!='3' ) as a  " +
            "WHERE user_code=#{userCode}")
    SystemUser findUserPartyByUserCode(String userCode) throws Exception;

    @Select("select * from sys_user where id=#{id}")
    SystemUser queryById(String id)throws Exception;

    @Select(" select * from sys_user where email=#{email}")
    List<SystemUser> queryByEmail(String email) throws Exception;

    @Select(" select * from sys_user where card_number=#{cardNumber} ")
    SystemUser queryByCardNumber(String cardNumber)throws Exception;

    @Select(" select * from sys_user where work_number=#{workNumber} ")
    SystemUser queryByWorkNumber(String workNumber)throws Exception;


    @Select(" select * from sys_user where user_code=#{userCode}")
    SystemUser queryByUserCode(String userCode)throws Exception;

    @Update(" update sys_user set user_state=#{userState},update_time=#{updateTime} where id=#{id} ")
    public int updateUserState(Map systemUser) throws Exception;

    @Update(" update sys_user set phone=#{newphone} where id=#{id} ")
    public int updateUserPhone(Map systemUser) throws Exception;

    @Update(" update sys_user set version=#{version} where id=#{id} ")
    public int updateUserVersion(SystemUser systemUser) throws Exception;

    @Update(" update sys_user set password=#{newpwd} where id=#{id} ")
    public int updateUserPwd(Map systemUser) throws Exception;

    @Update(" update party_user set party_id=#{newPartyId} where user_id=#{userId} ")
    public int updateUserParty(Map partyUser) throws Exception;


    @Select(" select * from sys_version where islate='1' ")
    SysVersion getVersion() throws Exception;

    @Select(" select IFNULL(mark,0) as mark from sys_user where id=#{id} ")
    Map<String, Object> getMarkById(Long id);

    @Select(" select * from sys_user where user_code=#{userCode}")
    List<SystemUser> queryUserCodeList(String userCode)throws Exception;

    @Update(" update sys_user set user_code=#{userCode} where id=#{id} ")
    public void updateCode(SystemUser s);

    /**
     * 获取某个单位下某个角色人员
     * @param map
     * @return
     * @throws Exception
     */
    @Select("SELECT DISTINCT CONCAT(A.user_name) userName FROM sys_user A\n" +
            "              INNER JOIN SYS_USER_ROLE B ON A.ID=B.USER_ID\n" +
            "              INNER JOIN SYSTEM_ROLE C ON B.ROLE_ID=C.ID\n" +
            "              INNER JOIN party_user D ON D.user_id =A.id\n" +
            "WHERE C.ROLE_NAME LIKE #{roleName} AND D.party_id = #{partyId}")
    List<String> getManagerUserName(Map<String, Object> map)throws Exception;
}
